package com.mall.product.service.impl;

import com.mall.entity.TbType;
import com.mall.product.mapper.TypeMapper;
import com.mall.product.service.TypeService;
import com.mall.product.vo.VoCcascade;
import com.mall.product.vo.VoType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务类型impl
 *
 * @author ASUS
 * @date 2021/07/18
 */
@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Override
    @Cacheable(cacheNames = {"typeList"})
    public List<TbType> findListByPid(Integer id){
        return typeMapper.findListByPid(id);
    }

    /**
     *  商品分类列表
     * @return
     */
    @Override
    public List<TbType> findAll() {
        List<TbType> typeList = typeMapper.findAll();
        List<TbType> list = typeList.stream().filter(type -> {
            return type.getParentId() == 0;
        }).map(current -> {
            current.setChildList(getChildren(current, typeList));
            return current;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 添加分类
     * @param root 根分类
     * @param all 全部
     * @return 子分类列表
     */
    private List<TbType> getChildren(TbType root,List<TbType> all) {
        List<TbType> childrenList = all.stream().filter(chiled -> {
            return chiled.getParentId() == root.getId().intValue();
        }).map(child -> {
            child.setChildList(getChildren(child, all));
            return child;
        }).collect(Collectors.toList());
        return childrenList;
    }

    /**
     * 查询商品级别分类
     * @param tbType
     * @return
     */
    @Override
    public List<VoType> findTypeById(TbType tbType) {
        List<VoType> typeById = typeMapper.findShopType(tbType);
        typeById.stream().forEach(type ->{
            List<VoType> typeById1 = typeMapper.findTypeById(type.getId());
            type.setList(typeById1);
        });
        return typeById;
    }

    /**
     * 商品根据id查询
     * @param id
     * @return
     */
    @Override
    public TbType findOne(Integer id) {
        return typeMapper.findOne(id);
    }

    /**
     * 添加商品
     * @param tbType
     */
    @Override
    public void addType(TbType tbType) {
        typeMapper.addType(tbType);
    }

    /**
     * 修改商品
     * @param tbType
     */
    @Override
    public void updateType(TbType tbType) {
        typeMapper.updateType(tbType);
    }

    /**
     * 商品删除
     * @param id
     */
    @Override
    public void delete(Integer id) {
        typeMapper.delete(id);
    }

    /**
     * 模糊查询
     * @param type
     * @return
     */
    @Override
    public List<VoType> findShopType(TbType type) {
        return typeMapper.findShopType(type);
    }

    /**
     * 查询父级pid
     * @param pid
     * @return
     */
    @Override
    public List<VoType> findTypePid(Integer pid) {
        return typeMapper.findTypePid(pid);
    }

    /**
     * 根据id修改状态
     * @param id
     * @param status
     */
    @Override
    public void upStatus(Integer id, Integer status) {
        typeMapper.upStatus(id,status);
    }

    /**
     * 添加父类
     * @param tbType
     */
    @Override
    public void add(TbType tbType) {
        typeMapper.add(tbType);
    }

    /**
     * 级联选择器
     * @param pid
     * @return
     */
    @Override
    public List<VoCcascade> findId(Integer pid) {
        List<VoCcascade>type=typeMapper.findId(pid);
        type.stream().forEach(list ->{
            list.setList(typeMapper.findId(list.getId()));
        });
        return type;
    }

}
